import { generateID, isInArray } from "@/utils";
import { filter } from "lodash";
import creep from "..";

export default class CreepWar extends Creep {
    
    public HandleAW():void{
        
    }
    public HandleGW():void{
        
    }
    public HandleSW():void{

    }
    /* 处理pb挖矿任务 */
    public HandlePB():void{
        if (!this.memory.bindTask) return
        var thisTask = Game.rooms[this.memory.belong].getMisson_ID(this.memory.bindTask)
        if (!thisTask) {delete this.memory.bindTask;return}
        if (!this.memory.disRoom) this.memory.disRoom = thisTask.disRoom[0]
        if (thisTask.bindData['power-heal'].bind.length <= 0 || thisTask.bindData['power-attack'].bind.length <= 0){this.say('??',true);return}
        /* 寻找对象 */
        if (!this.memory.geezTo)
        {
            if (this.memory.role == 'power-heal')
            {
                var geezCreep = this.pos.findClosestByPath(FIND_CREEPS,{filter:(creep)=>{
                    return creep.memory.role == 'power-attack' && (!creep.memory.geezTo || creep.memory.geezTo == this.name)
                }})
                if (geezCreep)
                this.memory.geezTo = geezCreep.name
                else
                this.say("?",true)
            }
            else if (this.memory.role == 'power-attack')
            {
                var geezCreep = this.pos.findClosestByPath(FIND_CREEPS,{filter:(creep)=>{
                    return creep.memory.role == 'power-heal' && (!creep.memory.geezTo || creep.memory.geezTo == this.name)
                }})
                if (geezCreep)
                this.memory.geezTo = geezCreep.name
                else
                this.say("?",true)
            }
            return
        }
        if (!Game.creeps[this.memory.geezTo] && this.room.name != this.memory.belong)
            this.suicide()
        if (!this.memory.geez)
        {
            this.say('集合!')
            /* 前往指定地点组队 */
            var pos:RoomPosition
            var geezFlag = Game.rooms[this.memory.belong].find(FIND_FLAGS,{filter:(flag)=>{
                return flag.color == COLOR_BLUE
            }})
            if (geezFlag.length < 1) pos = Game.rooms[this.memory.belong].controller.pos
            else pos = geezFlag[0].pos
            if (!this.pos.isNearTo(pos)){this.moveTo(pos);return}
            if (this.pos.isNearTo(Game.creeps[this.memory.geezTo]))
            {
                this.memory.geez = true
                Game.creeps[this.memory.geezTo].memory.geez = true
            }
            return
        }
        /* 已经组队的情况下 */
        if (this.memory.role == 'power-heal')
        {
            var disCreep = Game.creeps[this.memory.geezTo]
            if (!disCreep) this.suicide()
            this.moveTo(disCreep.pos,{ignoreCreeps:true})
            if (disCreep.hits < disCreep.hitsMax) this.heal(disCreep)
            else if (this.hits < this.hitsMax) this.heal(this)
        }
        else if (this.memory.role == 'power-attack')
        {
            this.memory.standed = true
            var disRoom = this.memory.disRoom
            if (this.room.name != disRoom || isInArray([0,49],this.pos.x) || isInArray([0,49],this.pos.y))
            {
                if (!this.pos.isNearTo(Game.creeps[this.memory.geezTo]) && (this.pos.y != 49 && this.pos.x != 0 && this.pos.y != 0 && this.pos.x != 49 ))return
                this.farMoveTo(new RoomPosition(25,25,disRoom),20)
            }
            else
            {
                if (!this.pos.isNearTo(Game.creeps[this.memory.geezTo])) return
                /* 攻击pb逻辑 */
                var disStructure = Game.getObjectById(thisTask.disStructure[0]) as Structure
                if (!disStructure)
                    disStructure = this.pos.getClosestStructure([STRUCTURE_POWER_BANK],0)
                if (!disStructure)
                {
                        Game.notify(`房间${this.name}的PB攻击任务结束,已经删除任务!`)
                        Game.rooms[this.memory.belong].deleteMisson('CREEP','pb攻击')
                        this.suicide()
                }
                var flag = Game.flags[`${this.memory.belong}/PB`]
                if (!flag) {disStructure.pos.createFlag(`${this.memory.belong}/PB`,COLOR_RED,COLOR_CYAN)}
                if (!this.pos.isNearTo(disStructure)) this.moveTo(disStructure)
                else
                    this.attack(disStructure)
            }
            
        }
    }
    /* 处理pb运输任务 */
    public HandlePBC():void{
        var thisTask = Game.rooms[this.memory.belong].getMisson('CREEP','pb运输')
        if (!thisTask) return
        var disRoom = thisTask.disRoom[0]
        this.workstate('power')
        var flag = Game.flags[`${this.memory.belong}/PB`]
        if (!flag) return
        if (this.memory.working)
        {
            if (this.room.name == this.memory.belong)
            {
                var storage_ = Game.getObjectById(this.room.memory.TaskMemory.storageID as string) as StructureStorage
                if (!storage_) return
                if (this.transfer(storage_,'power') == ERR_NOT_IN_RANGE)
                {
                    this.rush(storage_.pos)
                }
                else if (this.transfer(storage_,'power') == OK)
                {
                    this.suicide()
                }
            }
            else
            this.farMoveTo(new RoomPosition(25,25,this.memory.belong),20)
        }
        else
        {
            this.farMoveTo(flag.pos,4)
            if (this.room.name == disRoom)
            {
                var pb = flag.pos.GetStructure('powerBank')
                if (pb) return
                var ruins = flag.pos.lookFor(LOOK_RUINS)
                if (ruins.length > 0)
                {
                    if (this.withdraw(ruins[0],'power') == ERR_NOT_IN_RANGE)
                    {
                        this.moveTo(ruins[0])
                    }
                    return
                }
                var powers = flag.pos.lookFor(LOOK_RESOURCES)
                if (powers.length > 0)
                {
                    if (this.pickup(powers[0]) == ERR_NOT_IN_RANGE) this.moveTo(powers[0])
                    return
                }
                this.say("没东西了")
                if (this.store.getUsedCapacity('power') > 0)
                    this.memory.working = true
            }
            
        }
    }
    /* 处理攻防一体任务 */
    public HandleAIOA():void{
        if (!this.memory.bindTask) return
        var thisTask = Game.rooms[this.memory.belong].getMisson_ID(this.memory.bindTask)
        if (!thisTask) {delete this.memory.bindTask;return}
        this.memory.disRoom = thisTask.disRoom[0]
        var gFlag = this.pos.findClosestByRange(FIND_FLAGS,{filter:(flag)=>{
            return flag.color == COLOR_GREEN
        }})
        this.heal(this)
        if (gFlag)
        {
            this.moveTo(gFlag)
            if (this.pos.isEqualTo(gFlag.pos))
                gFlag.remove()
            return
        }
        if (this.room.name != this.memory.disRoom)
        {
            if (this.room.name == this.memory.belong)
            {
                if (thisTask.switch && !this.memory.boosted)
                {
                    var list = {
                        'ranged_attack':'XKHO2',
                        'move':'XZHO2',
                        'tough':'XGHO2',
                        'heal':'XLHO2'
                    }
                    for (var b of this.body)
                    {
                        if (!b.boost)
                        {
                            var rType = b.type
                            for (var i in thisTask.LabBind)
                            {
                                if (thisTask.LabBind[i][1] == list[rType])
                                {
                                    var disLab = Game.getObjectById(thisTask.LabBind[i][0]) as StructureLab
                                    if (!disLab) return
                                    if (!this.pos.isNearTo(disLab))
                                    {
                                        this.rush(disLab.pos)
                                    }
                                    else
                                    {
                                        disLab.boostCreep(this)
                                    }
                                    return
                                }
                            }
                            return
                        }
                    }
                    this.memory.boosted = true
                }
            }

            this.farMoveTo(new RoomPosition(25,25,this.memory.disRoom),20)
        }
        else
        {
            this.heal(this)
            this.rangedMassAttack()
            /* 爬虫战斗逻辑 */
            // this.moveTo(this.room.controller)
            // this.say("哈哈")
            var yFlag = this.pos.findClosestByPath(FIND_FLAGS,{filter:(flag)=>{
                return flag.color == COLOR_YELLOW
            }})
            if (yFlag)
            {
                if (yFlag.name == 'defend')
                {
                    var hc = this.pos.findClosestByRange(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
                        return !isInArray(Memory.whitesheets,creep.owner.username)
                    }})
                    if (hc)
                    {
                        if (this.rangedAttack(hc) == ERR_NOT_IN_RANGE) this.moveTo(hc)
                    }
                    else
                        if (!this.pos.isEqualTo(yFlag))
                        this.moveTo(yFlag)
                    return
                }
                var hostile_attack = this.pos.findInRange(FIND_HOSTILE_CREEPS,2,{filter:(creep)=>{
                    return creep.getActiveBodyparts('attack') > 0 || creep.getActiveBodyparts('ranged_attack') > 0
                }})
                if (hostile_attack.length > 0 && !this.pos.isEqualTo(yFlag))
                {
                    this.rangedAttack(hostile_attack[0])
                    this.moveTo(yFlag)
                    return
                }
            }
            var flag = this.pos.findClosestByPath(FIND_FLAGS,{filter:(flag)=>{
                return flag.color == COLOR_RED
            }})
            if (!flag)
            {
                var clostStructure = this.pos.findClosestByRange(FIND_HOSTILE_STRUCTURES,{filter:(struc)=>{
                    return !isInArray([STRUCTURE_CONTROLLER,STRUCTURE_WALL,STRUCTURE_RAMPART,STRUCTURE_STORAGE],struc.structureType)
                }})
                if (clostStructure)
                {
                    clostStructure.pos.createFlag(generateID(),COLOR_RED)
                    return
                }
                else
                {return}
            }
            if (!flag) return
            var hostile_Creep = this.pos.findInRange(FIND_HOSTILE_CREEPS,2,{filter:(creep)=>{
                return !creep.pos.GetStructure(STRUCTURE_RAMPART) && creep.getActiveBodyparts('attack') <= 0
            }})
            if (hostile_Creep.length > 0)
            {
                this.rangedAttack(hostile_Creep[0])
                return
            }
            var structures = flag.pos.lookFor(LOOK_STRUCTURES)
            if (structures.length > 0 || !this.pos.isNearTo(structures[0]))
            {
                this.moveTo(structures[0])

            }
            else
            {
                flag.remove()
            }
        }
    }
    /* 处理拆家一体任务 */
    public HandleDSOA():void{
        if (!this.memory.bindTask) return
        var thisTask = Game.rooms[this.memory.belong].getMisson_ID(this.memory.bindTask)
        if (!thisTask) {delete this.memory.bindTask;return}
        this.memory.disRoom = thisTask.disRoom[0]
        if (this.room.name != this.memory.disRoom)
        {
            this.heal(this)
            if (this.room.name == this.memory.belong)
            {
                if (thisTask.switch && !this.memory.boosted)
                {
                    var list = {
                        'work':'XZH2O',
                        'move':'XZHO2',
                        'tough':'XGHO2',
                        'heal':'XLHO2'
                    }
                    for (var b of this.body)
                    {
                        if (!b.boost)
                        {
                            var rType = b.type
                            for (var i in thisTask.LabBind)
                            {
                                if (thisTask.LabBind[i][1] == list[rType])
                                {
                                    var disLab = Game.getObjectById(thisTask.LabBind[i][0]) as StructureLab
                                    if (!disLab) return
                                    if (!this.pos.isNearTo(disLab))
                                    {
                                        this.rush(disLab.pos)
                                    }
                                    else
                                    {
                                        disLab.boostCreep(this)
                                    }
                                    return
                                }
                            }
                            return
                        }
                    }
                    this.memory.boosted = true
                }
            }
            this.farMoveTo(new RoomPosition(25,25,this.memory.disRoom),20)
        }
        else
        {
            var healpartNum = this.getActiveBodyparts('heal')
            var healNum = 12 * 4 * healpartNum
            var leastNum = Math.ceil(healNum / 100)
            // var toughNum = 0
            // for (var b of this.body)
            // {
            //     if (b.type == 'tough') toughNum ++
            // }
            if (this.getActiveBodyparts('tough') < (leastNum-4)) this.heal(this)
            /* 爬虫战斗逻辑 */
            // this.moveTo(this.room.controller)
            // this.say("哈哈")
            var yFlag = this.pos.findClosestByPath(FIND_FLAGS,{filter:(flag)=>{
                return flag.color == COLOR_YELLOW
            }})
            if (yFlag)
            {
                var hostile_attack = this.pos.findInRange(FIND_HOSTILE_CREEPS,2,{filter:(creep)=>{
                    return creep.getActiveBodyparts('attack') > 0 || creep.getActiveBodyparts('ranged_attack') > 0
                }})
                if (hostile_attack.length > 0 && !this.pos.isEqualTo(yFlag))
                {
                    //this.rangedAttack(hostile_attack[0])
                    this.moveTo(yFlag)
                    return
                }
            }
            var flag = this.pos.findClosestByRange(FIND_FLAGS,{filter:(flag)=>{
                return flag.color == COLOR_RED
            }})
            if (!flag)
            {
                var clostStructure = this.pos.findClosestByRange(FIND_HOSTILE_STRUCTURES,{filter:(struc)=>{
                    return !isInArray([STRUCTURE_CONTROLLER,STRUCTURE_WALL,STRUCTURE_RAMPART,STRUCTURE_STORAGE],struc.structureType)
                }})
                if (clostStructure)
                {
                    if (this.dismantle(clostStructure) == ERR_NOT_IN_RANGE) this.moveTo(clostStructure)
                    //clostStructure.pos.createFlag(generateID(),COLOR_RED)
                    return
                }
                else
                {return}
            }
            if (!flag) return
            // var hostile_Creep = this.pos.findInRange(FIND_HOSTILE_CREEPS,2,{filter:(creep)=>{
            //     return !creep.pos.GetStructure(STRUCTURE_RAMPART) && creep.getActiveBodyparts('attack') <= 0
            // }})
            // if (hostile_Creep.length > 0)
            // {
            //     this.rangedAttack(hostile_Creep[0])
            //     return
            // }
            // this.say("拆啊")
            var structures = flag.pos.lookFor(LOOK_STRUCTURES)
            if (structures.length > 0)
            {
                //this.say("要拆了")
                // console.log(structures[0].structureType)
                // console.log(this.dismantle(structures[0]))
                if (this.dismantle(structures[0]) == ERR_NOT_IN_RANGE || !this.pos.isNearTo(structures[0]))
                {
                    this.say("拆")
                    this.moveTo(structures[0])
                }
            }
            else
            {
                flag.remove()
            }
        }
    }
    /* 处理红球防御任务 */
    public HandleDR():void{
        if (!this.memory.bindTask) return
        var thisTask = Game.rooms[this.memory.belong].getMisson_ID(this.memory.bindTask)
        if (!thisTask) {delete this.memory.bindTask;return}
        if (!this.memory.boosted)
        {
            var list = {
                'move':'XZHO2',
                'attack':'XUH2O',
            }
            for (var b of this.body)
            {
                if (!b.boost)
                {
                    var rType = b.type
                    for (var i in thisTask.LabBind)
                    {
                        if (thisTask.LabBind[i][1] == list[rType])
                        {
                            var disLab = Game.getObjectById(thisTask.LabBind[i][0]) as StructureLab
                            if (!disLab) return
                            if (!this.pos.isNearTo(disLab))
                            {
                                this.rush(disLab.pos)
                            }
                            else
                            {
                                disLab.boostCreep(this)
                            }
                            return
                        }
                    }
                    return
                }
            }
            this.memory.boosted = true
        }
        /* 红球逻辑 */
        var BlueFlag = this.pos.findClosestByRange(FIND_FLAGS,{filter:(flag)=>{
            return flag.color == COLOR_BLUE
        }})
        if (BlueFlag)
        {
            if (this.pos != BlueFlag.pos)
                this.moveTo(BlueFlag)
            var hostile_creep = this.pos.findInRange(FIND_HOSTILE_CREEPS,1,{filter:(creep)=>{
                return !isInArray(Memory.whitesheets,creep.owner.username)
            }})
            if (hostile_creep && hostile_creep.length > 0) {this.attack(hostile_creep[0])}
            return
        }
        /* 寻找敌人 */
        var enemys = this.room.find(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
            return !isInArray(Memory.whitesheets,creep.owner.username)
        }})
        /* 没有敌人就去撤离点或者呆在原地 */
        if (enemys.length <= 0)
        {
            if (this.pos.GetStructure('rampart')) return
            if (Game.flags[`${this.memory.belong}/Retreat`]) this.moveTo(Game.flags[`${this.memory.belong}/Retreat`])
            else return
        }
        /* 计算自己的攻击力 */
        // var ATK = this.getActiveBodyparts('attack')*120
        // console.log("ATK:",ATK)
        /* 攻击红旗最近的敌人 */
        var RedFlag = this.pos.findClosestByRange(FIND_FLAGS,{filter:(flag)=>{
            return flag.color == COLOR_RED
        }})
        if (RedFlag)
        {
            var enemy = RedFlag.pos.findClosestByRange(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
                return !isInArray(Memory.whitesheets,creep.owner.username)
            }})
            if (enemy) this.attack_(enemy)
            else RedFlag.remove()
        }
        else
            return
        

    }
    /* 处理黄球拆迁任务 */
    public HandleDY():void{
        if (!this.memory.bindTask) return
        var thisTask = Game.rooms[this.memory.belong].getMisson_ID(this.memory.bindTask)
        if (!thisTask) {delete this.memory.bindTask;return}
        this.memory.disRoom = thisTask.disRoom[0]
        if (!this.memory.boosted)
        {
            var list = {
                'move':'XZHO2',
                'work':'XZH2O',
            }
            for (var b of this.body)
            {
                if (!b.boost)
                {
                    var rType = b.type
                    for (var i in thisTask.LabBind)
                    {
                        if (thisTask.LabBind[i][1] == list[rType])
                        {
                            var disLab = Game.getObjectById(thisTask.LabBind[i][0]) as StructureLab
                            if (!disLab) return
                            if (!this.pos.isNearTo(disLab))
                            {
                                this.rush(disLab.pos)
                            }
                            else
                            {
                                disLab.boostCreep(this)
                            }
                            return
                        }
                    }
                    return
                }
            }
            this.memory.boosted = true
        }
        if (this.room.name != this.memory.disRoom)
        {
            var gFlag = this.pos.findClosestByRange(FIND_FLAGS,{filter:(flag)=>{
                return flag.color == COLOR_BROWN
            }})
            if (gFlag)
            {
                this.moveTo(gFlag)
                if (this.pos.isEqualTo(gFlag.pos))
                    gFlag.remove()
                return
            }
            this.farMoveTo(new RoomPosition(25,25,this.memory.disRoom),20)
        }
        else
        {
            var yFlag = this.pos.findClosestByPath(FIND_FLAGS,{filter:(flag)=>{
                return flag.color == COLOR_YELLOW
            }})
            if (yFlag)
            {
                var hostile_attack = this.pos.findInRange(FIND_HOSTILE_CREEPS,2,{filter:(creep)=>{
                    return creep.getActiveBodyparts('attack') > 0 || creep.getActiveBodyparts('ranged_attack') > 0
                }})
                if (hostile_attack.length > 0 && !this.pos.isEqualTo(yFlag))
                {
                    this.moveTo(yFlag)
                    return
                }
            }
            var flag = this.pos.findClosestByRange(FIND_FLAGS,{filter:(flag)=>{
                return flag.color == COLOR_RED
            }})
            if (!flag)
            {
                var clostStructure = this.pos.findClosestByRange(FIND_HOSTILE_STRUCTURES,{filter:(struc)=>{
                    return !isInArray([STRUCTURE_CONTROLLER,STRUCTURE_WALL,STRUCTURE_RAMPART,STRUCTURE_STORAGE],struc.structureType)
                }})
                if (clostStructure)
                {
                    if (this.dismantle(clostStructure) == ERR_NOT_IN_RANGE) this.moveTo(clostStructure)
                    return
                }
                else
                {return}
            }
            if (!flag) return
            var structures = flag.pos.lookFor(LOOK_STRUCTURES)
            if (structures.length > 0)
            {
                if (this.dismantle(structures[0]) == ERR_NOT_IN_RANGE || !this.pos.isNearTo(structures[0]))
                {
                    this.say("拆")
                    this.moveTo(structures[0])
                }
            }
            else
            {
                flag.remove()
            }
        }

    }
}